﻿using System.Linq;
using System.Web.Mvc;
using Concordia.Models.cs_Registration;
using Concordia.Utilities;
using Concordia.ViewModels;

namespace Concordia.Controllers
{
    public class cs_RegistrationController : Controller
    {
        //
        // GET: /cs_Registration/

        public ActionResult cs_Registration_Page1()
        {
            return View();
        }

        [HttpPost]
        public ActionResult cs_Registration_Page1(cs_Registration_Page1 model)
        {
            var context = new cs_RegistrationContextDataContext();
            var result = context.WEB_SEL_BORROWER_REGISTRATION(model.LastName, model.SSN, model.AccountNumber).FirstOrDefault();
            
            if (result.OK_TO_REGISTER == true)
            {
                cs_RegistrationViewModel vm = new cs_RegistrationViewModel();
                vm.Page1Results = result;
                vm.RegistrationField_Page1 = model;
                Session["RegistrationViewModel"] = vm;
                return RedirectToAction("cs_Registration_Page2");
            }

            //todo mark failure
            return View();
        }

        public ActionResult cs_Registration_Page2()
        {

            var context = new cs_RegistrationContextDataContext();

            var vm = (cs_RegistrationViewModel)Session["RegistrationViewModel"];
            
            //Populate the dropdown's using this data...
            vm.SecurityQuestions = context.WEB_SEL_SECURITY_QUESTIONS().ToList();
            ViewBag.SecurityQuestion1 = new SelectList(vm.SecurityQuestions,"SECURITY_QUESTIONS_KEY","QUESTION", 1);
            ViewBag.SecurityQuestion2 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 7);
            ViewBag.SecurityQuestion3 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 12);
            
            return View(vm);
        }
        
        [HttpPost]
        public ActionResult cs_Registration_Page2(FormCollection form)
        {
            var vm = (cs_RegistrationViewModel)Session["RegistrationViewModel"];
            TryUpdateModel(vm.RegistrationField_Page2);
            
            if (ModelState.IsValid)
            {
                Session["RegistrationViewModel"] = vm; //maybe not necessary, but in the name of comleteness...

                var context = new cs_RegistrationContextDataContext();
                var result = context.WEB_INS_BORROWER_REGISTRATION(vm.Page1Results.BORROWER_KEY,
                                                                    vm.RegistrationField_Page2.Email,
                                                                    vm.RegistrationField_Page2.Username,
                                                                    vm.RegistrationField_Page2.Password,
                                                                    vm.RegistrationField_Page2.SecurityQuestion1,
                                                                    vm.RegistrationField_Page2.SecurityAnswer1,
                                                                    vm.RegistrationField_Page2.SecurityQuestion2,
                                                                    vm.RegistrationField_Page2.SecurityAnswer2,
                                                                    vm.RegistrationField_Page2.SecurityQuestion3,
                                                                    vm.RegistrationField_Page2.SecurityAnswer3
                                                                    ).FirstOrDefault();
                if (result.REG_OK == true)
                {
                    var email = new Emailer(vm.RegistrationField_Page2.Email);
                    email.SendWelcome(vm.RegistrationField_Page1.FirstName, vm.RegistrationField_Page2.Username); //todo: handle failed email
                    return RedirectToAction("cs_Logon", "cs_Logon");
                }
                else //TODO:  Include error message
                {
                    //need to reload the mofo data...
                    ViewBag.SecurityQuestion1 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 1);
                    ViewBag.SecurityQuestion2 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 7);
                    ViewBag.SecurityQuestion3 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 12);
                    return View(vm);
                }    
            }
        }

        private void BuildSecurityQuestionBad(cs_RegistrationViewModel vm)
        {
            ViewBag.SecurityQuestion1 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 1);
            ViewBag.SecurityQuestion2 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 7);
            ViewBag.SecurityQuestion3 = new SelectList(vm.SecurityQuestions, "SECURITY_QUESTIONS_KEY", "QUESTION", 12);
        }

    }
}
